package com.example.cloudmusic.home.repository

import com.example.cloudmusic.home.model.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext

/**
 * 模拟数据源策略实现
 * 提供模拟数据，用于开发测试或离线场景
 */
class MockDataSourceStrategy : DataSourceStrategy {

    /**
     * 获取轮播图数据
     */
    override suspend fun getBanners(): Result<List<Banner>> = withContext(Dispatchers.IO) {
        try {
            // 模拟网络延迟
            delay(300)
            
            // 返回模拟数据
            val banners = listOf(
                Banner(
                    id = "1",
                    imageUrl = "https://img0.baidu.com/it/u=744030493,1290779638&fm=253&fmt=auto&app=138&f=PNG?w=1333&h=500"
                ),
                Banner(
                    id = "2",
                    imageUrl = "https://img2.baidu.com/it/u=3346309070,2711000986&fm=253&fmt=auto&app=138&f=PNG?w=860&h=291"
                ),
                Banner(
                    id = "3",
                    imageUrl = "https://img2.baidu.com/it/u=3196607735,1854098938&fm=253&fmt=auto&app=138&f=JPEG?w=499&h=156"
                )
            )
            
            Result.success(banners.shuffled())
        } catch (e: Exception) {
            Result.failure(e)
        }
    }
    
    /**
     * 获取推荐歌单
     */
    override suspend fun getRecommendedPlaylists(): Result<List<Playlist>> = withContext(Dispatchers.IO) {
        try {
            // 模拟网络延迟
            delay(300)
            
            Result.success(getMockPlaylists(8))
        } catch (e: Exception) {
            Result.failure(e)
        }
    }
    
    /**
     * 获取热门艺人
     */
    override suspend fun getHotArtists(): Result<List<Artist>> = withContext(Dispatchers.IO) {
        try {
            // 模拟网络延迟
            delay(300)
            
            // 返回模拟数据
            val artists = listOf(
                Artist(
                    id = "artist_1",
                    name = "林俊杰",
                    imageUrl = "http://p2.music.126.net/r6W-zCnV-aduVn_PLZYuYg==/109951168529049969.jpg?param=130y130",
                    followers = 14700000,
                    musics = getMockMusics(5)
                ),
                Artist(
                    id = "artist_2",
                    name = "陶喆",
                    imageUrl = "http://p2.music.126.net/ZxYbcFQcEElDjER_5Pq6Tg==/109951170606719006.jpg?param=130y130",
                    followers = 6940000,
                    musics = getMockMusics(5)
                ),
                Artist(
                    id = "artist_3",
                    name = "颜人中",
                    imageUrl = "http://p2.music.126.net/ei0o1W2N_SX1vpkXr2DDtA==/109951165122695489.jpg?param=130y130",
                    followers = 10870000,
                    musics = getMockMusics(5)
                ),
                Artist(
                    id = "artist_4",
                    name = "王力宏",
                    imageUrl = "http://p2.music.126.net/T5eZ-cn4-_JAdsnB_WtHzQ==/109951170336953174.jpg?param=130y130",
                    followers = 25000000,
                    musics = getMockMusics(5)
                ),
                Artist(
                    id = "artist_5",
                    name = "陈奕迅",
                    imageUrl = "http://p2.music.126.net/ODuFZql3x08Q4AaW7y20Aw==/109951169014571694.jpg?param=130y130",
                    followers = 15800000,
                    musics = getMockMusics(5)
                )
            )
            
            Result.success(artists.shuffled())
        } catch (e: Exception) {
            Result.failure(e)
        }
    }
    
    /**
     * 获取个人歌单
     */
    override suspend fun getPersonalPlaylists(): Result<List<Playlist>> = withContext(Dispatchers.IO) {
        try {
            // 模拟网络延迟
            delay(300)
            
            Result.success(getMockPlaylists(5))
        } catch (e: Exception) {
            Result.failure(e)
        }
    }
    
    /**
     * 获取音乐风格
     */
    override suspend fun getMusicStyles(): Result<List<MusicStyle>> = withContext(Dispatchers.IO) {
        try {
            // 模拟网络延迟
            delay(300)
            
            // 返回模拟数据
            val musicStyles = listOf(
                MusicStyle(
                    style = "说唱",
                    imageUrl = "http://p2.music.126.net/tJUsQEQvh51WpP65bmaTCQ==/109951170091890381.jpg?param=150y150",
                    musics = getMockPlaylists(3)
                ),
                MusicStyle(
                    style = "古典",
                    imageUrl = "http://p2.music.126.net/urByD_AmfBDBrs7fA9-O8A==/109951167976973225.jpg?param=150y150",
                    musics = getMockPlaylists(3)
                ),
                MusicStyle(
                    style = "电音",
                    imageUrl = "http://p2.music.126.net/hXGObvXfsGtFjFvRhOYAkA==/109951170091888741.jpg?param=150y150",
                    musics = getMockPlaylists(3)
                ),
                MusicStyle(
                    style = "潮流",
                    imageUrl = "http://p1.music.126.net/dIKA5e7jCncz2Br1Toxgaw==/109951170621574552.jpg?param=150y150",
                    musics = getMockPlaylists(3)
                ),
                MusicStyle(
                    style = "国风",
                    imageUrl = "http://p1.music.126.net/kTJC5OBhg8I477X_ZmXyDQ==/109951168539740982.jpg?param=150y150",
                    musics = getMockPlaylists(3)
                )
            )
            
            Result.success(musicStyles.shuffled())
        } catch (e: Exception) {
            Result.failure(e)
        }
    }
    
    /**
     * 获取推荐歌曲
     */
    override suspend fun getRecommendedMusics(): Result<List<Music>> = withContext(Dispatchers.IO) {
        try {
            // 模拟网络延迟
            delay(300)
            
            Result.success(getMockMusics(24))
        } catch (e: Exception) {
            Result.failure(e)
        }
    }
    
    /**
     * 生成模拟歌单数据
     */
    private fun getMockPlaylists(count: Int): List<Playlist> {
        val titles = listOf(
            "听歌的人不寂寞", "好听的歌曲说唱版", "热恋的旋律，不一样的感受",
            "我喜欢的音乐", "周杰伦歌单", "适合深夜的轻音乐", "全世界失眠", "说谎",
            "我的名字", "情话", "感谢你曾来过", "起风了", "一路向北"
        )

        val artists = listOf(
            "林俊杰", "周杰伦", "邓紫棋", "陈奕迅", "五月天", "薛之谦", "阿肆", "TFBOYS"
        )

        val items = mutableListOf<Playlist>()
        repeat(count) { index ->
            items.add(
                Playlist(
                    id = "playlist_$index",
                    title = titles.random(),
                    artist = artists.random(),
                    imageUrl = playListUrl[index],
                    playCount = (50000..5000000).random().toLong(),
                    description = "精选歌单，为您推荐",
                    musics = getMockMusics(10)
                )
            )
        }
        return items.shuffled()
    }

    val playListUrl = mutableListOf("http://p1.music.126.net/wUE3qX3qb39T7ZPzFV7T0Q==/109951170621274487.jpg?param=140y140",
        "http://p1.music.126.net/Q5l_A05RXLED-O-TTN2wZQ==/109951170382467892.jpg?param=140y140",
        "http://p1.music.126.net/8fQ9jzTJnMweLNm7VoyPSw==/6623458045881301.jpg?param=140y140",
        "http://p1.music.126.net/TstZwNu49sz7tPiRi9JvXw==/109951170299850301.jpg?param=140y140",
        "http://p1.music.126.net/nMZRCV3g1M91QI6k0vZuKw==/109951170622621251.jpg?param=140y140",
        "http://p1.music.126.net/FOpAT55rIsILSXeCNem_jQ==/109951170439734763.jpg?param=140y140",
        "http://p1.music.126.net/l-dkNzgVNectM6v1VTw9VA==/109951170516793627.jpg?param=140y140",
        "http://p1.music.126.net/MiRWotNVEmB8HRp1mhTudw==/109951170447697156.jpg?param=140y140",
        )
    
    /**
     * 生成模拟歌曲数据
     */
    private fun getMockMusics(count: Int): List<Music> {
        
        // 使用日志中的真实歌曲数据
        val musics = listOf(
            Music(
                id = "1",
                title = "苦咖啡·唯一",
                artist = "那奇沃夫 / KKECHO",
                album = "苦咖啡·唯一",
                imageUrl = "http://p1.music.126.net/8DkTnzi7jdjWGYl4qbwLCg==/109951164517295956.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://blog-img-bitebyte.oss-cn-chengdu.aliyuncs.com/audio/Ref%EA%9E%89rain-Aimer.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            Music(
                id = "2",
                title = "他只是经过",
                artist = "高旭",
                album = "他只是经过",
                imageUrl = "https://p1.music.126.net/wUog39IHoJb76pL0AVCFNQ==/109951165348116023.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://m704.music.126.net/20250322223347/3e74f07721e3339e0ca8b7f0c443ec23/jdyyaac/obj/w5rDlsOJwrLDjj7CmsOj/34290289568/a90c/e7d6/a9f6/991328eccc8f3ff75fbb3fda328b728a.m4a",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            Music(
                id = "3",
                title = "晚风",
                artist = "BT07",
                album = "晚风",
                imageUrl = "https://p1.music.126.net/lCblKUB1hLND5FxiVI1_Lw==/109951164919449758.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://m704.music.126.net/20250322223347/3e74f07721e3339e0ca8b7f0c443ec23/jdyyaac/obj/w5rDlsOJwrLDjj7CmsOj/34290289568/a90c/e7d6/a9f6/991328eccc8f3ff75fbb3fda328b728a.m4a",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            Music(
                id = "4",
                title = "有些",
                artist = "颜人中",
                album = "有些",
                imageUrl = "http://p1.music.126.net/8DkTnzi7jdjWGYl4qbwLCg==/109951164517295956.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://m704.music.126.net/20250322223347/3e74f07721e3339e0ca8b7f0c443ec23/jdyyaac/obj/w5rDlsOJwrLDjj7CmsOj/34290289568/a90c/e7d6/a9f6/991328eccc8f3ff75fbb3fda328b728a.m4a",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            Music(
                id = "5",
                title = "7%",
                artist = "XMASwu",
                album = "7%",
                imageUrl = "https://p2.music.126.net/YsXfYs7s2-d0lJwRtp_klQ==/109951164700110582.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://music.163.com/song/media/outer/url?id=1423241987.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            Music(
                id = "6",
                title = "毒药",
                artist = "SN",
                album = "毒药",
                imageUrl = "http://p2.music.126.net/iNiPTo2kIsyytjpZ_gYhBA==/109951168894259186.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://music.163.com/song/media/outer/url?id=2071596484.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            Music(
                id = "7",
                title = "在一起叫梦",
                artist = "mmm",
                album = "在一起叫梦",
                imageUrl = "http://p1.music.126.net/wNkdszCwZuzZOs-1GoHM4Q==/109951168763488576.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://music.163.com/song/media/outer/url?id=2023954033.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            Music(
                id = "8",
                title = "独行侠+爱如潮水",
                artist = "sea蕊",
                album = "独行侠+爱如潮水",
                imageUrl = "http://p2.music.126.net/ra554YmMoOBw8_8xDiDZNg==/109951166276735546.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://music.163.com/song/media/outer/url?id=1432968475.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            Music(
                id = "9",
                title = "下坠Falling",
                artist = "刘宗鑫",
                album = "下坠Falling",
                imageUrl = "http://p1.music.126.net/TyQNalBMYwsCOl5sq_NBTA==/109951164573566845.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://music.163.com/song/media/outer/url?id=1328146041.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            Music(
                id = "10",
                title = "HEAVEN",
                artist = "恩主",
                album = "HEAVEN",
                imageUrl = "http://p1.music.126.net/23n5WHHKF0bI5CmHueAC_w==/109951168657850338.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://music.163.com/song/media/outer/url?id=2052988645.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            Music(
                id = "11",
                title = "夏夜最后的烟火",
                artist = "颜人中",
                album = "夏夜最后的烟火",
                imageUrl = "http://p2.music.126.net/i1p1CCz6PaDF2Gs0RqKJBQ==/109951165349625690.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://music.163.com/song/media/outer/url?id=1482867143.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            Music(
                id = "12",
                title = "你的酒馆对我打了烊",
                artist = "陈雪凝",
                album = "你的酒馆对我打了烊",
                imageUrl = "http://p1.music.126.net/LiRR__0pJHSivqBHZzbMUw==/109951163816225567.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://music.163.com/song/media/outer/url?id=1341964346.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            Music(
                id = "13",
                title = "11",
                artist = "队长",
                album = "11",
                imageUrl = "http://p2.music.126.net/zaLqUvz-C_H1u4n8-0MQcw==/109951166868150028.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://music.163.com/song/media/outer/url?id=2023954033.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            Music(
                id = "14",
                title = "予你",
                artist = "队长",
                album = "予你",
                imageUrl = "http://p1.music.126.net/yxVm_vRFOode6yP67NmMcA==/109951166625738075.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://music.163.com/song/media/outer/url?id=1895330088.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            Music(
                id = "15",
                title = "浪漫主义",
                artist = "姜云升",
                album = "浪漫主义",
                imageUrl = "http://p2.music.126.net/lFFiY-jvO1ENHtK2bZ6eUw==/109951166531420475.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://music.163.com/song/media/outer/url?id=1887917182.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            Music(
                id = "16",
                title = "经济舱",
                artist = "刘聪",
                album = "经济舱",
                imageUrl = "http://p2.music.126.net/fEOnFVVXSAe2LThsNfx0XA==/109951165432413857.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://music.163.com/song/media/outer/url?id=1492049185.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            Music(
                id = "17",
                title = "可不可以",
                artist = "张紫豪",
                album = "可不可以",
                imageUrl = "http://p1.music.126.net/WafK2OQfEtqXitdDXJ772Q==/109951163252847249.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://music.163.com/song/media/outer/url?id=553755659.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            Music(
                id = "18",
                title = "算了吧",
                artist = "刘思达",
                album = "算了吧",
                imageUrl = "http://p1.music.126.net/imN8WzDuirHVdFm4-E3ofA==/109951166871655381.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://music.163.com/song/media/outer/url?id=1908049566.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            Music(
                id = "19",
                title = "风驶过的声音是",
                artist = "海洋Bo",
                album = "风驶过的声音是",
                imageUrl = "http://p2.music.126.net/gfLR-3TEncp1cOog8oUaFA==/109951168567036973.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://music.163.com/song/media/outer/url?id=2041974276.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            Music(
                id = "20",
                title = "愿与愁",
                artist = "林俊杰",
                album = "愿与愁",
                imageUrl = "http://p2.music.126.net/vtnI8JpimWnZSzkXdmIB3w==/109951168558210782.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://music.163.com/song/media/outer/url?id=1905004937.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            Music(
                id = "21",
                title = "哪里都是你 (0.8x)",
                artist = "苏亦洋",
                album = "哪里都是你 (0.8x)",
                imageUrl = "http://p1.music.126.net/KHBdIrV8pBeC25IVfJuwGg==/109951163093094139.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://music.163.com/song/media/outer/url?id=1973864158.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            Music(
                id = "22",
                title = "凄美地",
                artist = "郭顶",
                album = "凄美地",
                imageUrl = "http://p2.music.126.net/wSMfGvFzOAYRU_yVIfquAA==/2946691248081599.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://music.163.com/song/media/outer/url?id=436346833.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            Music(
                id = "23",
                title = "这一生关于你的风景（女生版）",
                artist = "是二智呀",
                album = "这一生关于你的风景（女生版）",
                imageUrl = "http://p1.music.126.net/4WRCmAazeh2BpXSpQC45rg==/109951164192553640.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://music.163.com/song/media/outer/url?id=1375941658.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            ),
            Music(
                id = "24",
                title = "雪Distance",
                artist = "罗言",
                album = "雪Distance",
                imageUrl = "http://p2.music.126.net/cGTKkFl5qcXIDYPo7PrmDA==/109951168431655039.jpg?param=130y130",
                duration = 180000,
                playUrl = "https://music.163.com/song/media/outer/url?id=2026224214.mp3",
                isVip = false,
                isFavorite = false,
                type = "流行"
            )
        )
        
        // 根据请求的数量返回相应数量的歌曲
        return musics.take(count).shuffled()
    }
} 